<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限操作</title>
    <script src="../../../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
    <link href="../../../../res/echarts/eleTree.css" rel="stylesheet"/>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../res/images/favicon.ico">
    <script src="../../../../res/echarts/eleTree.js"></script>
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <style type="text/css">
        body {
            padding: 10px;
        }
    </style>
</head>
<body>
<form class="layui-form layui-form-pane" action="" id="jurisdictionForm" style="display: none">
    <input type="hidden" id="pkJurisdictionId" name="pkJurisdictionId">
    <div class="layui-form-item">
        <div class="layui-form-item" pane="" id="jurisdictionTypes">
            <label class="layui-form-label">权限类型</label>
            <div class="layui-input-block">
                <input type="checkbox" checked="" name="open" lay-skin="switch" lay-filter="switchTest" title="开关" lay-text="菜单|权限">
                <input id="jurisdictionType" name="jurisdictionType" type="hidden" value="1" />
            </div>
        </div>
        <div class="layui-inline" id="superior">
            <label class="layui-form-label">上级菜单</label>
            <div class="layui-input-inline">
                <select name="jurisdictionSuperior" id="jurisdictionSuperior" lay-filter="aihao">
                    <option value="0">请选择上级菜单</option>

                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">权限名称</label>
            <div class="layui-input-inline">
                <input type="text" name="jurisdictionName" id="jurisdictionName" lay-verify="required" placeholder="请输入权限名称（必填）"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">权限代码</label>
            <div class="layui-input-inline">
                <input type="text" name="jurisdictionCode" id="jurisdictionCode" lay-verify="required" placeholder="请输入权限代码（必填）"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item" id="jurisdictionUrls">
        <label class="layui-form-label">权限地址</label>
        <div class="layui-input-block">
            <input type="text" name="jurisdictionUrl" id="jurisdictionUrl" autocomplete="off" placeholder="请输入地址" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" id="jurisdictionDescribe" name="jurisdictionDescribe" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1" id="sub">立即提交</button>
        </div>
    </div>
</form>
<script>
    $.ajaxSetup({
        async: false
    });
    layui.use(['form', 'layedit', 'laydate', 'layer'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;


        const pkJurisdictionId = getQueryString("pkJurisdictionId");
        const show=getQueryString("show");

        $("#jurisdictionCode").on("blur",function () {
            if($("#jurisdictionCode").val().length>0){
                const res=getAjaxById(getRootPath()+"OaJurisdictionAction/isExist/"+$("#jurisdictionCode").val());
                if(res>0){
                        layer.alert("权限代重复", {
                            icon: 2,
                        },function () {
                            layer.msg('请重新输入', {icon: 0});
                            $("#jurisdictionCode").val("");
                            $("#jurisdictionCode").focus();
                        });
                }
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function(data){
            if(this.checked){
                $("#superior").show();
                $("#jurisdictionUrls").show();
                $("#jurisdictionType").val("1");
                form.render();
            }else{
                $("#superior").hide();
                $("#jurisdictionUrls").hide();
                $("#jurisdictionUrl").val("");
                $("#jurisdictionType").val("0");
                $("#jurisdictionSuperior").val("");
                form.render();
            }
        });
        let url;
        if(pkJurisdictionId){
            url=getRootPath()+"OaJurisdictionAction/selectAll/"+pkJurisdictionId;
        }else{
            url= getRootPath()+"OaJurisdictionAction/selectAll/0";
        }
        const jurisdictionSuperior=getAjaxById(url);
        if(jurisdictionSuperior.length>0){
            $("#jurisdictionSuperior").html('<option value="0">请选择上级菜单</option>');
            $.each(jurisdictionSuperior,function (index,item) {
                $("#jurisdictionSuperior").append('<option value="'+item.pkJurisdictionId+'">'+item.jurisdictionName+'</option>');
            })
        }else{
            $("#jurisdictionSuperior").html('<option value="0">暂无上级菜单</option>');
        }

        //监听提交
        form.on('submit(demo1)', function (data) {
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            let res=postAjax(getRootPath()+"OaJurisdictionAction/addOrUpdate",data.field);
            if(res.code===1){
                parent.layui.table.reload('test');//刷新父页面表格数据
                layer.msg("操作成功", {icon: 1});
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            }else{
                layer.msg("操作失败");
            }
            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
            alert(JSON.stringify(data));
        });

        if(pkJurisdictionId){
            $("#pkJurisdictionId").val(pkJurisdictionId);
            $("#jurisdictionTypes").hide();
            let res=getAjaxById(getRootPath()+'OaJurisdictionAction/findById/'+pkJurisdictionId);
            if(res){
                $("#jurisdictionName").val(res.jurisdictionName);
                $("#jurisdictionCode").val(res.jurisdictionCode);
                $("#jurisdictionDescribe").val(res.jurisdictionDescribe);
                if(res.jurisdictionType==="0"){
                    $("#superior").hide();
                    $("#jurisdictionUrls").hide();
                    $("#jurisdictionUrl").val("");
                    $("#jurisdictionType").val("0");
                    $("#jurisdictionSuperior").val("");
                }else{
                    $("#jurisdictionSuperior").val(res.jurisdictionSuperior);
                    $("#jurisdictionUrl").val(res.jurisdictionUrl);
                }
            }
            if(show){
                $("#jurisdictionSuperior").attr("disabled","");
                $("#jurisdictionUrl").attr("disabled","");
                $("#jurisdictionCode").attr("disabled","");
                $("#jurisdictionDescribe").attr("disabled","");
                $("#jurisdictionName").attr("disabled","");
                $("#sub").hide();
            }
        }

        $("#jurisdictionForm").show();
        form.render();
    });
</script>
</body>
</html>